package com.simon.mengkou.system.global;

import android.app.Application;

import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.cache.CacheFactory;
import com.simon.mengkou.data.db.DaoFactory;
import com.simon.mengkou.data.enums.EAccountType;
import com.simon.mengkou.data.pref.OuerPreferences;
import com.simon.mengkou.future.base.OuerHeader;
import com.simon.mengkou.future.impl.OuerFuture;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.utils.UtilImage;
import com.simon.mengkou.utils.UtilOuer;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.PlatformConfig;
import com.xiaomi.mipush.sdk.MiPushClient;

import io.vov.vitamio.Vitamio;

/**
 * @作者 Zhenshui.Xia
 * @日期 2014年8月5日
 * @描述 应用Application
 */
public class OuerApplication extends Application {
    //app实例
    public static OuerApplication mInstance;
    //当前用户信息
    public static User mUser;
    //应用信息
    public static OuerHeader mAppInfo;
    //应用任务实例
    public static OuerFuture mOuerFuture;
    //全局sp实例
    public static OuerPreferences mPreferences;
    //缓存factory实例
    public static CacheFactory mCacheFactory;
    //Dao factory实例
    public static DaoFactory mDaoFactory;
    //图片加载
    public static UtilImage mImageLoader;


    @Override
    public void onCreate() {
        super.onCreate();

        String processName = UtilOuer.getProcessName(this);
        if (UtilString.isNotBlank(processName)
                && getPackageName().equals(processName)) {
            //接口地址
            if(CstOuer.DEBUG) {
                CstOuer.API_URL = CstOuer.API_DEBUG_URL;
                CstOuer.H5_URL = CstOuer.H5_DEBUG_URL;
            } else {
                CstOuer.API_URL = CstOuer.API_ONLINE_URL;
                CstOuer.H5_URL = CstOuer.H5_ONLINE_URL;
            }

            mInstance = this;
            //应用任务实例
            mOuerFuture = new OuerFuture(this, CstOuer.PROJECT, CstOuer.DEBUG, CstOuer.COOKIE);
            //应用信息
            mAppInfo = UtilOuer.getOuerHeader(this);
            //全局sp实例
            mPreferences = new OuerPreferences(this);
            //缓存factory实例
            mCacheFactory = CacheFactory.getInstance(this);
            //Dao factory实例
            mDaoFactory = DaoFactory.getInstance(this);
            //获取当前用户信息
            String userId = mPreferences.getUserId();
            mUser = mDaoFactory.getUserDao().getUser(userId);
//            mUser = new User();
//            mUser.setUserId("111");
//            mUser.setNick("基督教的");
//            mUser.setType(EAccountType.PHONE);
//            mUser.setAvatarUrl("http://n.sinaimg.cn/top/20160314/E-7A-fxqhmvp6177642.jpg");
            //图片加载实例
            mImageLoader = UtilImage.getInstance(this);

            //初始化推送
            initPush();
            //初始化统计
            initStat();
            //初始化友盟社交
            initSocialize();

            Vitamio.isInitialized(this);
        }
    }

    /**
     * 初始化推送
     */
    private void initPush() {
        //注册push服务，注册成功后会向MiPushMessageReceiver发送广播
        MiPushClient.registerPush(this,
                CstOuer.MIPUSH.MIPUSH_APP_ID,
                CstOuer.MIPUSH.MIPUSH_APP_KEY);
    }

    /**
     * 初始化统计
     */
    private void initStat() {
        //设置是否对日志信息进行加密, 默认false(不加密)
        AnalyticsConfig.enableEncrypt(true);
        //异常捕获
        MobclickAgent.setCatchUncaughtExceptions(true);
    }

    /**
     * 初始化友盟社交
     */
    private void initSocialize() {
        //微信 appid appsecret
        PlatformConfig.setWeixin(CstOuer.WECHAT.WECHAT_APP_ID,
                CstOuer.WECHAT.WECHAT_APP_KEY);

        //新浪微博 appkey appsecret
        PlatformConfig.setSinaWeibo(CstOuer.SINA.SINAWEIBO_APP_KEY,
                CstOuer.SINA.SINAWEIBO_APP_SECRET);

        // QQ和Qzone appid appkey
        PlatformConfig.setQQZone(CstOuer.TENCENT.TENCENT_APP_ID,
                CstOuer.TENCENT.TENCENT_APP_KEY);
    }

}
